package com.cy.store.Controller;

import com.cy.store.Controller.ex.*;
import com.cy.store.Service.Ex.*;
import com.cy.store.util.JsonResult;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.xml.crypto.dsig.keyinfo.KeyInfoFactory;

public class BaseController {
    //表示操作成功的状态码
    public static int OK=200;
    //这个方法的返回值时传递给前端的数据
    //自动将异常对象传递给此方法的参数列表上
    @ExceptionHandler({ServiceException.class,FileUploadException.class})//用于统一处理抛出的异常
    public JsonResult<Void> handleException(Throwable e){
        JsonResult<Void> result=new JsonResult<>();
        if(e instanceof UsernameDuplicatedException){
            result.setState(4000);
            result.setMessage("用户名已被占用");
        }
        else if(e instanceof UserNotFoundException){
            result .setState(4001);
            result.setMessage("用户数据不存在的异常");
        } else if (e instanceof PasswordNotMatchException) {
            result.setState(4002);
            result.setMessage("用户名的密码错误");
        } else if(e instanceof InsertException){
            result.setState(4004);
            result.setMessage("注册时产生未知的异常");
        } else if(e instanceof UpdateException){
            result.setState(4003);
            result.setMessage("更新数据时产生了未知的异常");
        }else if(e instanceof AddressCountLimitException){
            result.setState(4005);
            result.setMessage("用户的收货地址数量超出上限");
        }
        else if (e instanceof FileEmptyException) {
            result.setState(6000);
            result.setMessage("上传空文件异常");
        } else if (e instanceof FileSizeException) {
            result.setState(6001);
            result.setMessage("上传文件大小异常");
        } else if (e instanceof FileTypeException) {
            result.setState(6002);
            result.setMessage("上传文件类型异常");
        } else if (e instanceof FileStateException) {
            result.setState(6003);
            result.setMessage("上传文件状态异常");
        } else if (e instanceof FileUploadIOException) {
            result.setState(6004);
            result.setMessage("文件IO流异常");
        }else if (e instanceof AddressNotFoundException) {
            result.setState(5000);
            result.setMessage("用户的收货地址数据不存在的异常");
        } else if (e instanceof AccessDeniedException) {
            result.setState(5001);
            result.setMessage("收货地址数据非法访问的异常");
        }else if (e instanceof DeleteException) {
            result.setState(5002);
            result.setMessage("删除数据时产生未知的异常");
        } else if (e instanceof ProductNotFoundException) {
            result.setState(4006);
            result.setMessage("访问的商品数据不存在的异常");
        }else if (e instanceof CartNotFoundException) {
            result.setState(4007);
            result.setMessage("购物车表不存在该商品的异常");
        }


        return result;
    }

    /**
     * 获取session对象的uid
     * @param session
     * @returnda当前登录的用的uid的值
     */
    //封装session
    protected final Integer getuidFromSession(HttpSession session){
       return  Integer.valueOf(session.getAttribute("uid").toString());
    }

    protected final String getusernameFromSession(HttpSession session){
        return session.getAttribute("username").toString();
    }
}
